package com.lkz.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lkz.service.IOrdersService;
import com.lkz.entity.Orders;
import com.lkz.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 *  前端控制器
 */
@RestController
public class OrdersController {
    @Autowired
    private IOrdersService ordersService;
    @GetMapping("/orders")
    public Map getList(Integer page, Integer limit){
        Map<String,Object> map = new HashMap<String,Object>();
        Page<Orders> pages = new Page<Orders>();
        pages.setSize(limit);
        pages.setCurrent(page);
        IPage<Orders> iPage = ordersService.selectList(pages);
        map.put("msg","");
        map.put("count",iPage.getTotal());
        map.put("data",iPage.getRecords());
        map.put("code",0);
        return map;
    }
    @GetMapping("/orders/byDel")
    public Map byDel(Integer page, Integer limit){
        Map<String,Object> map = new HashMap<String,Object>();
        Page<Orders> pages = new Page<Orders>();
        pages.setSize(limit);
        pages.setCurrent(page);
        IPage<Orders> iPage = ordersService.selectDelList(pages);
        map.put("msg","");
        map.put("count",iPage.getTotal());
        map.put("data",iPage.getRecords());
        map.put("code",0);
        return map;
    }
    @PostMapping("/orders")
    public Map add(Orders orders){
        Map<String,Object> result = new HashMap<String,Object>();
        orders.setCreateTime(StringUtils.getNowTime());
        orders.setUpdateTime(orders.getCreateTime());
        orders.setIsDel(0);
        result.put("state",ordersService.save(orders));
        return result;
    }
    @PutMapping("/orders")
    public Map update(Orders orders){
        Map<String,Object> result = new HashMap<String,Object>();
        orders.setUpdateTime(StringUtils.getNowTime());
        result.put("state",ordersService.updateById(orders));
        return result;
    }
    @DeleteMapping("/orders/del/{ordersId}")
    public Map del(@PathVariable Integer ordersId){
        Map<String,Object> result = new HashMap<String,Object>();
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("orders_id",ordersId);
        Orders orders = new Orders();
        orders.setIsDel(1);
        result.put("state",ordersService.update(orders,queryWrapper));
        return result;
    }
    @DeleteMapping("/orders/recover/{ordersId}")
    public Map recover(@PathVariable Integer ordersId){
        Map<String,Object> result = new HashMap<String,Object>();
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("orders_id",ordersId);
        Orders orders = new Orders();
        orders.setIsDel(0);
        result.put("state",ordersService.update(orders,queryWrapper));
        return result;
    }
}
